home *** CD-ROM | disk | FTP | other *** search
Wrap
# Source Generated with Decompyle++ # File: in.pyc (Python 2.4) ''' Test cases for the repr module Nick Mathewson ''' import sys import os import unittest from test.test_support import run_unittest from repr import repr as r def nestedTuple(nesting): t = () for i in range(nesting): t = (t,) return t class ReprTests(unittest.TestCase): def test_string(self): eq = self.assertEquals eq(r('abc'), "'abc'") eq(r('abcdefghijklmnop'), "'abcdefghijklmnop'") s = 'a' * 30 + 'b' * 30 expected = repr(s)[:13] + '...' + repr(s)[-14:] eq(r(s), expected) eq(r('"\''), repr('"\'')) s = '"' * 30 + "'" * 100 expected = repr(s)[:13] + '...' + repr(s)[-14:] eq(r(s), expected) def test_container(self): array = array import array deque = deque import collections eq = self.assertEquals eq(r(()), '()') eq(r((1,)), '(1,)') eq(r((1, 2, 3)), '(1, 2, 3)') eq(r((1, 2, 3, 4, 5, 6)), '(1, 2, 3, 4, 5, 6)') eq(r((1, 2, 3, 4, 5, 6, 7)), '(1, 2, 3, 4, 5, 6, ...)') eq(r([]), '[]') eq(r([ 1]), '[1]') eq(r([ 1, 2, 3]), '[1, 2, 3]') eq(r([ 1, 2, 3, 4, 5, 6]), '[1, 2, 3, 4, 5, 6]') eq(r([ 1, 2, 3, 4, 5, 6, 7]), '[1, 2, 3, 4, 5, 6, ...]') eq(r(set([])), 'set([])') eq(r(set([ 1])), 'set([1])') eq(r(set([ 1, 2, 3])), 'set([1, 2, 3])') eq(r(set([ 1, 2, 3, 4, 5, 6])), 'set([1, 2, 3, 4, 5, 6])') eq(r(set([ 1, 2, 3, 4, 5, 6, 7])), 'set([1, 2, 3, 4, 5, 6, ...])') eq(r(frozenset([])), 'frozenset([])') eq(r(frozenset([ 1])), 'frozenset([1])') eq(r(frozenset([ 1, 2, 3])), 'frozenset([1, 2, 3])') eq(r(frozenset([ 1, 2, 3, 4, 5, 6])), 'frozenset([1, 2, 3, 4, 5, 6])') eq(r(frozenset([ 1, 2, 3, 4, 5, 6, 7])), 'frozenset([1, 2, 3, 4, 5, 6, ...])') eq(r(deque([ 1, 2, 3, 4, 5, 6, 7])), 'deque([1, 2, 3, 4, 5, 6, ...])') eq(r({ }), '{}') d = { 'alice': 1, 'bob': 2, 'charles': 3, 'dave': 4 } eq(r(d), "{'alice': 1, 'bob': 2, 'charles': 3, 'dave': 4}") d['arthur'] = 1 eq(r(d), "{'alice': 1, 'arthur': 1, 'bob': 2, 'charles': 3, ...}") eq(r(array('i')), "array('i', [])") eq(r(array('i', [ 1])), "array('i', [1])") eq(r(array('i', [ 1, 2])), "array('i', [1, 2])") eq(r(array('i', [ 1, 2, 3])), "array('i', [1, 2, 3])") eq(r(array('i', [ 1, 2, 3, 4])), "array('i', [1, 2, 3, 4])") eq(r(array('i', [ 1, 2, 3, 4, 5])), "array('i', [1, 2, 3, 4, 5])") eq(r(array('i', [ 1, 2, 3, 4, 5, 6])), "array('i', [1, 2, 3, 4, 5, ...])") def test_numbers(self): eq = self.assertEquals eq(r(123), repr(123)) eq(r(0x7BL), repr(0x7BL)) eq(r(1.0 / 3), repr(1.0 / 3)) n = 0xAL ** 100 expected = repr(n)[:18] + '...' + repr(n)[-19:] eq(r(n), expected) def test_instance(self): eq = self.assertEquals i1 = ClassWithRepr('a') eq(r(i1), repr(i1)) i2 = ClassWithRepr('x' * 1000) expected = repr(i2)[:13] + '...' + repr(i2)[-14:] eq(r(i2), expected) i3 = ClassWithFailingRepr() eq(r(i3), '<ClassWithFailingRepr instance at %x>' % id(i3)) s = r(ClassWithFailingRepr) self.failUnless(s.startswith('<class ')) self.failUnless(s.endswith('>')) self.failUnless(s.find('...') == 8) def test_file(self): fp = open(unittest.__file__) self.failUnless(repr(fp).startswith("<open file '%s', mode 'r' at 0x" % unittest.__file__)) fp.close() self.failUnless(repr(fp).startswith("<closed file '%s', mode 'r' at 0x" % unittest.__file__)) def test_lambda(self): self.failUnless(repr((lambda x: x)).startswith('<function <lambda')) def test_builtin_function(self): eq = self.assertEquals eq(repr(hash), '<built-in function hash>') self.failUnless(repr(''.split).startswith('<built-in method split of str object at 0x')) def test_xrange(self): import warnings eq = self.assertEquals eq(repr(xrange(1)), 'xrange(1)') eq(repr(xrange(1, 2)), 'xrange(1, 2)') eq(repr(xrange(1, 2, 3)), 'xrange(1, 4, 3)') def test_nesting(self): eq = self.assertEquals eq(r([ [ [ [ [ [ []]]]]]]), '[[[[[[[]]]]]]]') eq(r([ [ [ [ [ [ [ []]]]]]]]), '[[[[[[[...]]]]]]]') eq(r(nestedTuple(6)), '(((((((),),),),),),)') eq(r(nestedTuple(7)), '(((((((...),),),),),),)') eq(r({ nestedTuple(5): nestedTuple(5) }), '{((((((),),),),),): ((((((),),),),),)}') eq(r({ nestedTuple(6): nestedTuple(6) }), '{((((((...),),),),),): ((((((...),),),),),)}') eq(r([ [ [ [ [ [ { }]]]]]]), '[[[[[[{}]]]]]]') eq(r([ [ [ [ [ [ [ { }]]]]]]]), '[[[[[[[...]]]]]]]') def test_buffer(self): x = buffer('foo') self.failUnless(repr(x).startswith('<read-only buffer for 0x')) def test_cell(self): pass def test_descriptors(self): eq = self.assertEquals eq(repr(dict.items), "<method 'items' of 'dict' objects>") class C: def foo(cls): pass x = staticmethod(C.foo) self.failUnless(repr(x).startswith('<staticmethod object at 0x')) x = classmethod(C.foo) self.failUnless(repr(x).startswith('<classmethod object at 0x')) def touch(path, text = ''): fp = open(path, 'w') fp.write(text) fp.close() def zap(actions, dirname, names): for name in names: actions.append(os.path.join(dirname, name)) class LongReprTest(unittest.TestCase): def setUp(self): longname = 'areallylongpackageandmodulenametotestreprtruncation' self.pkgname = os.path.join(longname) self.subpkgname = os.path.join(longname, longname) os.mkdir(self.pkgname) touch(os.path.join(self.pkgname, '__init__' + os.extsep + 'py')) os.mkdir(self.subpkgname) touch(os.path.join(self.subpkgname, '__init__' + os.extsep + 'py')) self.here = os.getcwd() sys.path.insert(0, self.here) def tearDown(self): actions = [] os.path.walk(self.pkgname, zap, actions) actions.append(self.pkgname) actions.sort() actions.reverse() for p in actions: if os.path.isdir(p): os.rmdir(p) continue os.remove(p) del sys.path[0] def test_module(self): eq = self.assertEquals touch(os.path.join(self.subpkgname, self.pkgname + os.extsep + 'py')) areallylongpackageandmodulenametotestreprtruncation = areallylongpackageandmodulenametotestreprtruncation import areallylongpackageandmodulenametotestreprtruncation.areallylongpackageandmodulenametotestreprtruncation eq(repr(areallylongpackageandmodulenametotestreprtruncation), "<module '%s' from '%s'>" % (areallylongpackageandmodulenametotestreprtruncation.__name__, areallylongpackageandmodulenametotestreprtruncation.__file__)) eq(repr(sys), "<module 'sys' (built-in)>") def test_type(self): eq = self.assertEquals touch(os.path.join(self.subpkgname, 'foo' + os.extsep + 'py'), 'class foo(object):\n pass\n') foo = foo import areallylongpackageandmodulenametotestreprtruncation.areallylongpackageandmodulenametotestreprtruncation eq(repr(foo.foo), "<class '%s.foo'>" % foo.__name__) def test_object(self): pass def test_class(self): touch(os.path.join(self.subpkgname, 'bar' + os.extsep + 'py'), 'class bar:\n pass\n') bar = bar import areallylongpackageandmodulenametotestreprtruncation.areallylongpackageandmodulenametotestreprtruncation self.failUnless(repr(bar.bar).startswith('<class %s.bar at 0x' % bar.__name__)) def test_instance(self): touch(os.path.join(self.subpkgname, 'baz' + os.extsep + 'py'), 'class baz:\n pass\n') baz = baz import areallylongpackageandmodulenametotestreprtruncation.areallylongpackageandmodulenametotestreprtruncation ibaz = baz.baz() self.failUnless(repr(ibaz).startswith('<%s.baz instance at 0x' % baz.__name__)) def test_method(self): eq = self.assertEquals touch(os.path.join(self.subpkgname, 'qux' + os.extsep + 'py'), 'class aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa:\n def amethod(self): pass\n') qux = qux import areallylongpackageandmodulenametotestreprtruncation.areallylongpackageandmodulenametotestreprtruncation eq(repr(qux.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.amethod), '<unbound method aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.amethod>') iqux = qux.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa() self.failUnless(repr(iqux.amethod).startswith('<bound method aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.amethod of <%s.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa instance at 0x' % (qux.__name__,))) def test_builtin_function(self): pass class ClassWithRepr: def __init__(self, s): self.s = s def __repr__(self): return 'ClassWithLongRepr(%r)' % self.s class ClassWithFailingRepr: def __repr__(self): raise Exception('This should be caught by Repr.repr_instance') def test_main(): run_unittest(ReprTests) if os.name != 'mac': run_unittest(LongReprTest) if __name__ == '__main__': test_main()